package io.framework.modules.pollutionsource.controller;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import io.framework.common.annotation.LogOperation;
import io.framework.common.constant.Constant;
import io.framework.common.exception.ErrorCode;
import io.framework.common.page.PageData;
import io.framework.common.utils.ExcelUtils;
import io.framework.common.utils.Result;
import io.framework.common.validator.AssertUtils;
import io.framework.common.validator.ValidatorUtils;
import io.framework.common.validator.group.AddGroup;
import io.framework.common.validator.group.DefaultGroup;
import io.framework.common.validator.group.UpdateGroup;
import io.framework.modules.pollutionsource.dto.TBasPollutionlabeldirDTO;
import io.framework.modules.pollutionsource.excel.TBasPollutionlabeldirExcel;
import io.framework.modules.pollutionsource.service.TBasPollutionlabeldirService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 基础-污染源标签年份表名录（重点排污单位，环统单位）
 *
 * @author Gujq email
 * @since 1.0.0 2020-03-24
 */
@RestController
@Api(tags="基础-污染源标签年份表名录（重点排污单位，环统单位）")
public class TBasPollutionlabeldirController {
    @Autowired
    private TBasPollutionlabeldirService tBasPollutionlabeldirService;

    @GetMapping("pollutionsource/tbaspollutionlabeldir/page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String"),
        @ApiImplicitParam(name = "keyName", value = "关键字", paramType = "query", dataType="String"),
    })
    @RequiresPermissions("pollutionsource:tbaspollutionlabeldir:page")
    public Result<PageData<TBasPollutionlabeldirDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){

        PageData<TBasPollutionlabeldirDTO> page = tBasPollutionlabeldirService.pages(params);

        return new Result<PageData<TBasPollutionlabeldirDTO>>().ok(page);
    }

    @GetMapping("pollutionsource/tbaspollutionlabeldir/{id}")
    @ApiOperation("信息")
    public Result<TBasPollutionlabeldirDTO> get(@PathVariable("id") String id){
        TBasPollutionlabeldirDTO data = tBasPollutionlabeldirService.selectById(id);

        return new Result<TBasPollutionlabeldirDTO>().ok(data);
    }

    @PostMapping("pollutionsource/tbaspollutionlabeldir/save")
    @ApiOperation("保存")
    @LogOperation("添加重点排污单位信息")
    @RequiresPermissions("pollutionsource:tbaspollutionlabeldir:save")
    public Result save(TBasPollutionlabeldirDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        dto.setPkDirid(IdWorker.get32UUID());
        dto.setCreatetime(new Date());
        tBasPollutionlabeldirService.save(dto);

        return new Result();
    }

    @PutMapping("pollutionsource/tbaspollutionlabeldir/update")
    @ApiOperation("修改")
    @LogOperation("修改重点排污单位信息")
    @RequiresPermissions("pollutionsource:tbaspollutionlabeldir:update")
    public Result update(TBasPollutionlabeldirDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        dto.setUpdatetime(new Date());
        tBasPollutionlabeldirService.update(dto);

        return new Result();
    }

    @DeleteMapping("pollutionsource/tbaspollutionlabeldir/delete/{id}")
    @ApiOperation("删除")
    @LogOperation("删除重点排污单位信息")
    @RequiresPermissions("pollutionsource:tbaspollutionlabeldir:delete")
    public Result delete(@PathVariable("id") String id){
        //效验数据
        AssertUtils.isBlank("id",ErrorCode.IDENTIFIER_NOT_NULL);

        tBasPollutionlabeldirService.deleteById(id);

        return new Result();
    }

}